#ifndef _MT5_GAME_H_
#define _MT5_GAME_H_

#include "mat_logicboard.h"
#include "mat_define.h"
#include "mat_arrow.h"
#include "mat_sparkle.h"
#include "ui/ui_number.h"
#include "editor/editor_main.h"
#include "input/keyboard.h"
#include "input/mouse.h"
#include "ui/ui_sprite.h"
#include "part2d/part2d.h"

class MATGameEditor;

namespace gameflow
{
	class UIPopup;
};

using namespace editor;

class MATGame : public GameBase
{
public:

	

	const static int	RENDER_BUFFER = 125;

	struct RenderList
	{
		ui::UISprite*	m_pSprite;
		math::Vec2		m_Pos;
		math::Vec2		m_Size;
		COLOR			m_Color;
		float			m_Rotation;
		bool			m_IsAfterTutorial;
	};

	struct AnimateBoard
	{
		enum BOARD_STATE
		{
			BOARD_STATE_NONE,
			BOARD_STATE_SHOW,
			BOARD_STATE_SHOWING,
			BOARD_STATE_HIDE,
			BOARD_STATE_HIDING,
			BOARD_STATE_C,
		};

		AnimateBoard();

		void			Hide(float Dealy);
		void			Show(float Delay);
		void			Tick(float dt);
		void			Reset();

		math::Vec2		m_Offset;		
		float			m_Accel;

		float			m_Delay;
		BOARD_STATE		m_State;
	};

	

	MATGame();
	~MATGame();

	
	static const math::Vec2&	GetBoardPos(int y,int x)  { return m_BoardPos[y][x]; };	
	static const math::Vec2&	GetGemSize ()  { return m_SizeGemStatic; };

	static ui::UISprite*		GetSpriteByGem(MATGem* Gem);

	void						Render();
	void						RenderDebug();
	void						PostRender();	
	void						RenderOverUI();
	void						Tick(float dt);
	
	void						KeyPressed(input::Keyboard::KEY* pKey);
	void						MouseButtonDown(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos);
	void						MouseButtonReleased(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos);

	void						MouseButtonPressed(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos);	
	void						MouseMove(const math::Vec2& From, const math::Vec2& To);
	void						MouseWheelMove(int WheelInfo, const math::Vec2& Pos);

	virtual bool				Save(const std::string& File);	// save to filename
	virtual bool				Load(const std::string& File);		// load from filename
	virtual bool				Clear();		// clear everything in the game
	void						ClearRenderList();

	ui::UISprite*				GetTileSprite(int Type);	
	void						GetBoardXY(const math::Vec2& Pos, int& X, int& Y);
	void						SetPausePopup(gameflow::UIPopup* pPopup) { m_pUIPause = pPopup; };
	void						BuildRenderList();

	void						SetBackGround(const std::string& FileName);
	void						SetBackGround();
	float						GetScale() const { return m_Scale; };

	void						AnimateHideBoard(float Delay);
	void						AnimateShowBoard(float Delay);	

protected:

	struct						ChestPart2D
	{
		part2d::Part2D			m_PartChestTreasure;
		float					m_ReSpawnPart;	
		math::Vec2				m_SpawnPos;

		void					Tick(float dt, float Scale);
		void					Load(const std::string& File, float Scale);

		ChestPart2D() : m_ReSpawnPart(0){};

	};

	
	void						AddToRenderList(int& Index, 												
												ui::UISprite* pSprite, 
												MATGem*	pGem,
												const math::Vec2 Pos, 
												const math::Vec2 Size, 												
												COLOR c = ColorfRGBA(1.0f,1.0f,1.0f,1.0f), 
												float Rotation = 0);

	void						RenderTile(bool IsRenderOverGem);
	void						ShowNotValidMove(int Y, int X);

	
	static ui::UISprite			m_SprGem[GEM_UNIQUE_COUNT];
	static ui::UISprite			m_SprGemGlow[GEM_UNIQUE_COUNT];
	static ui::UISprite			m_SprBomb[GEM_UNIQUE_COUNT];
	static ui::UISprite			m_SprBombGlow[GEM_UNIQUE_COUNT];	
	
	ui::UISprite				m_SprBombFire;		
	ui::UISprite				m_BoardFrame;
	ui::UISprite				m_BackGround;
	ui::UISprite				m_SprBoard_BG;
	
	ui::UISprite				m_SprTimeBonus;
	ui::UISprite				m_SprNotValidMove;
	
	ui::UISprite				m_SprTiles[TILE_COUNT];

	static math::Vec2			m_BoardPos[MAX_COUNT_Y][MAX_COUNT_X];	
	static math::Vec2			m_SizeGemStatic;
	float						m_Scale;

	math::Vec2					m_SizeGem;
	math::Vec2					m_SizeGemSmall;	

	RenderList					m_RenderList[RENDER_BUFFER];
	MATArrow					m_Arrow;
	int							m_PathMoveIndex;

	MATSparkle					m_Sparkle;
	
	MATGameEditor*				m_pEditor;		
	math::Vec2					m_AspectScale;
	math::Vec2					m_BoardStart;

	float						m_NotValideMoveShowDelay;
	gameflow::UIPopup*			m_pUIPause;
	gameflow::UIPopup*			m_pUISetting;
	ui::UINumber				m_BombTurn;

	AnimateBoard				m_AnimateBoard;

};






#endif // _MT5_GAME_H_